<?php
header("Content-type:text/html; charset=utf-8");
function weather($args, $content, &$smarty){
	$city_id=$args['city_id'];
	$num=$args['num'];
	$result=array();
	$num=($num>0&&$num<7)?$num:6;
		$weather_info=@file_get_contents("http://m.weather.com.cn/data/$city_id.html");
		//$weather_info=@file_get_contents("data/$city_id.txt");
		$weather_info=json_decode($weather_info,"true");
		if (json_last_error()){
			$smarty->assign("weather",$result);
			return $content;
		}
		$weather=$weather_info['weatherinfo'];
		$result['city']=$weather['city'];
		$result['city_en']=$weather['city_en'];
		$result['city_id']=$weather['cityid'];
		$everyday=array();
		$date_today=str_replace(array("年","月"), "-",$weather['date_y']);
		$date_today=strtotime(str_replace("日", "",$date_today));
		$week_array=array("日","一","二","三","四","五","六");
		$week_today=date("w",$date_today);
				for($i=0;$i<$num;$i++){
					$everyday[$i]['date']=date("Y年m月d日",$date_today+$i*24*3600);
					$everyday[$i]['week']="星期".$week_array[($week_today+$i)%7];
					$everyday[$i]['temperature']=$weather["temp".($i+1)];
					$everyday[$i]['fahrenheit']=$weather["tempF".($i+1)];
					$everyday[$i]['weather']=$weather["weather".($i+1)];
					$everyday[$i]['day_img']=$weather["img".(2*$i+1)];
					$everyday[$i]['night_img']=$weather["img".(2*$i+2)];
					$everyday[$i]['day_img_title']=$weather["img_title".(2*$i+1)];
					$everyday[$i]['night_img_title']=$weather["img_title".(2*$i+2)];
					$everyday[$i]['wind']=$weather["wind".($i+1)];
					$everyday[$i]['wind_power']=$weather["fl".($i+1)];
				}
				$result['everyday']=$everyday;
		$smarty->assign("weather",$result);
		return $content;
}
